package org.balabala.user.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.balabala.user.entity.BrowseInfoEntity;
import org.balabala.user.entity.UserEntity;
import org.balabala.user.entity.VideoEntity;
import org.balabala.user.service.BrowseInfoService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *浏览信息
 * @author hanshanxi
 * @email 3176988793@qq.com
 * @date 2021-09-04 09:31:53
 */
@RestController
@RequestMapping("/balabala/browse")
public class BrowseInfoController {
    @Autowired
    private BrowseInfoService browseInfoService;

    /**
     * 分页查询全部信息
     */
    @RequestMapping("/list")
    public Resp list(@RequestParam Map<String, Object> params){
        PageUtils page = browseInfoService.queryPage(params);

        return Resp.ok().put("page", page);
    }

    /**
     * 模糊查询
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @RequestMapping("/batchlist")
    public Resp batchList(@RequestParam Integer page,@RequestParam Integer limit,@RequestParam String key){
        Map<String,Object> map = new HashMap<>();
        map.put("key",key);
        map.put("page",page.toString());
        map.put("limit",limit.toString());
        PageUtils pageUtils = browseInfoService.batchList(map);
        return Resp.ok().put("page",pageUtils);
    }


    //TODO
    //通过用户id查询浏览信息
    @RequestMapping("info")
    public Resp listByUid(@RequestParam Long uid,@RequestParam Integer page, @RequestParam Integer limit){
        Map<String,Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("page",page.toString());
        map.put("limit",limit.toString());

        List<VideoEntity> videoEntities = browseInfoService.queryPageByUid(map);
        if(videoEntities != null){
           return  Resp.ok().put("data",videoEntities);
       }else{
           return Resp.error(500,"查询失败，请确定是否参与评论");
       }
    }

    //通过视频id查询评论信息
    @RequestMapping("listByVideoId")
    public Resp listByVideoId(@RequestParam Map<String, Object> params){
        List<UserEntity> page = browseInfoService.queryPageByVideoId(params);
        if(page != null){
            return  Resp.ok().put("data",page);
        }else{
            return Resp.error(500,"查询失败，该视频暂无浏览记录");
        }
    }
    //查询视频的浏览量
  /*  @PostMapping("/browseCount")
    public Resp browseCount(@RequestBody Long videoid){
        Long count = browseInfoService.browseCount(videoid);
    }*/
    /**
     * 信息
     */
    @RequestMapping("/info/browseId")

    public Resp info(@RequestParam  Long browseId){
		BrowseInfoEntity browseInfo = browseInfoService.getById(browseId);

        return Resp.ok().put("browseInfo", browseInfo);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")

    public Resp save(@RequestBody BrowseInfoEntity browseInfo){
		browseInfoService.saveBrowse(browseInfo);

        return Resp.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public Resp update(@RequestBody BrowseInfoEntity browseInfo){
		browseInfoService.updateById(browseInfo);

        return Resp.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/batchdelete")

    public Resp delete(@RequestBody Integer[] browseIds){
		browseInfoService.removeByIds(Arrays.asList(browseIds));
        return Resp.ok();
    }

    @RequestMapping("/delete")
    public Resp delete(@RequestBody Integer browseId){
       return  browseInfoService.removeByBrowseId(browseId);
    }

}
